package com.px.lovepath.utils.network;

import java.util.LinkedList;

import org.apache.http.HttpHost;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Looper;
import android.text.TextUtils;

public class NetworkMonitor extends BroadcastReceiver {
	private static NetworkMonitor mSingleton = null;
	private final Context mContext;
	private final ConnectivityManager mConnectivityManager;
	private boolean mIsNetworkConnected = false;
	private final LinkedList<NetworkListener> mListenerList = new LinkedList<NetworkListener>();
	
	// ### 构造函数 ###
	protected NetworkMonitor(Context context) {
		mContext = context;
		mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		assert mConnectivityManager != null;
		
		mIsNetworkConnected = isNetworkConnected();
		IntentFilter filter = new IntentFilter();
		filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		context.registerReceiver(this, filter);
	
	}
	
	// ### BroadcastReceiver方法重写 ###
	@Override
	public void onReceive(Context context, Intent intent) {
		boolean isNetworkConnected = isNetworkConnected();
		if (mIsNetworkConnected != isNetworkConnected) {
			mIsNetworkConnected = isNetworkConnected;
			
			if (mIsNetworkConnected)
				notifyNetworkConnected();
			else
				notifyNetworkDisconnected();
		}
	}

	// ### 属性 ###
	public boolean getIsWifiConnected() {
		return isWifiConnected();
	}
	public boolean getIsNetworkConnected() {
		return isNetworkConnected();
	}
	public HttpHost getHttpProxy() {
		String proxy = null;
		String port = null;
		
		Uri uri = Uri.parse("content://telephony/carriers/preferapn");
		Cursor cursor = mContext.getContentResolver().query(uri, null, null, null, null);
		while (cursor != null && cursor.moveToNext()) {
			int index = cursor.getColumnIndex("proxy");
			if (index >= 0) {
				proxy = cursor.getString(index);
			}
			index = cursor.getColumnIndex("port");
			if (index >= 0) {
				port = cursor.getString(index);
			}
		}
		
		if (TextUtils.isEmpty(proxy) == false && TextUtils.isEmpty(port) == false) {
			return new HttpHost(proxy, Integer.valueOf(port));
		}
		
		return null;
	}

	// ### 方法 ###
	public static void startup(Context context) {
		assert Thread.currentThread() == Looper.getMainLooper().getThread();
		assert mSingleton == null;
		
		mSingleton = new NetworkMonitor(context);
	}
	public static NetworkMonitor get() {
		assert mSingleton != null;
		return mSingleton;
	}
	public void addNetworkListener(NetworkListener listener) {
		mListenerList.add(listener);
	}
	public void removeNetworkListener(NetworkListener listener) {
		mListenerList.remove(listener);
	}

	// ### 实现函数 ###
	private boolean isWifiConnected() {
		if (isNetworkConnected() == false)
			return false;
		
		NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
		return info.getType() == ConnectivityManager.TYPE_WIFI;
	}
	private boolean isNetworkConnected() {
		NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
		if (info == null)
			return false;
		
		if (info.isAvailable() && info.isConnected())
			return true;
		
		return false;
	}
	private void notifyNetworkConnected() {
		for (NetworkListener listener : mListenerList) {
			assert listener != null;
			listener.onNetworkConnected(this);
		}
	}
	private void notifyNetworkDisconnected() {
		for (NetworkListener listener : mListenerList) {
			assert listener != null;
			listener.onNetworkDisconnected(this);
		}
	}
}
